package com.swgk.core.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.swgk.core.FragmentLifeCycle;
import com.swgk.core.R;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.GlideEngine;
import com.swgk.core.view.weiget.BigHomeComparyWidget;

import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * Description:
 * Author zhengkewen
 * Time 2017/12/11
 */
public class AppFragment extends BaseFragment {

    protected View mAllView;
    protected RelativeLayout mErrorView;
    protected View mDisplayView;
    protected View mBottomView;
    protected FragmentLifeCycle fragmentLifeCycle;

    protected ViewStub vsCompany;
    protected BigHomeComparyWidget bigHomeCompary;
    protected LinearLayout inflatedCompany;

    public void setFragmentLifeCycle(FragmentLifeCycle fragmentLifeCycle){
        this.fragmentLifeCycle = fragmentLifeCycle;
        getLifecycle().addObserver(fragmentLifeCycle);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (fragmentLifeCycle != null){
            fragmentLifeCycle.onFragmentHiddenChanged(isVisibleToUser);
        }
    }

    @Override
    protected boolean registerNet() {
        return false;
    }

    @Override
    protected void connectNetWork() {

    }

    @Override
    protected void disconnectNetWork() {

    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }

    @Override
    public void onMultiClick(View v) {

    }

    protected void closeKeybord() {
        if (getActivity().getCurrentFocus() != null) {
            ((InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getActivity().getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mAllView = view.findViewById(R.id.success_view);
        mErrorView = view.findViewById(R.id.error_view);
        mDisplayView = view.findViewById(R.id.display_view);
    }

    public void loading() {
        if (mAllView != null && mErrorView != null) {
            mAllView.setVisibility(View.GONE);
            mErrorView.setVisibility(View.GONE);
        }
    }

    public void error() {
        if (mErrorView != null && mAllView != null) {
            mAllView.setVisibility(View.VISIBLE);
            mErrorView.setVisibility(View.VISIBLE);
            if (mDisplayView != null) {
                mDisplayView.setVisibility(View.GONE);
            }
        }
    }

    public void success() {
        if (mErrorView != null && mAllView != null) {
            mAllView.setVisibility(View.VISIBLE);
//            mErrorView.setVisibility(View.GONE);
        }
    }

    /**
     * 调用本地相册获取相册照片(调用pictureSelector()方法后再实现onPictureSelectorResult方法来获取图片的回调)
     *
     * @param isCompress 是否压缩图片
     * @param size       最小压缩图片的的大小
     * @param number     还能选择图片的数量
     */
    protected void pictureSelector(boolean isCompress, int size, int number) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.MULTIPLE)
                .isCompress(isCompress)
                .minimumCompressSize(size)
                .maxSelectNum(number)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    /**
     * 调用本地相册获取相册照片(调用pictureSelector()方法后再实现pictureSelectorResult方法来获取图片的回调)
     *
     * @param isCompress  是否压缩图片
     * @param size        最小压缩图片的的大小
     * @param number      还能选择图片的数量
     * @param requestCode 请求code用来判断多次图片获取的位置
     */
    protected void pictureSelector(boolean isCompress, int size, int number, int requestCode) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.MULTIPLE)
                .isCompress(isCompress)
                .minimumCompressSize(size)
                .maxSelectNum(number)
                .forResult(requestCode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pictureSelectorResult(requestCode, resultCode, data);
    }

    /**
     * 获取本地相册照片的回调
     *
     * @param
     * @return
     */
    protected void pictureSelectorResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            onPictureSelectorResult(PictureSelector.obtainMultipleResult(data));
        }
    }

    /**
     * 获取本地相册照片的回调
     *
     * @param
     * @return
     */
    protected void onPictureSelectorResult(List<LocalMedia> selectList) {

    }
}


